package com.xiaoke.util.exception;

import com.xiaoke.util.common.AjaxMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(CustomException.class)
    public AjaxMsg handleRRException(CustomException e){
        return new AjaxMsg(e.getCode(),e.getMsg());
    }


    @ExceptionHandler(AuthorizationException.class)
    public AjaxMsg handleAuthorizationException(AuthorizationException e){
        log.error(e.getMessage(), e);
        return AjaxMsg.error("没有权限，请联系管理员授权！");
    }

    @ExceptionHandler(Exception.class)
    public AjaxMsg handleException(Exception e){
        log.error(e.getMessage(), e);
        return AjaxMsg.error("未知异常，请联系管理员!");
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public AjaxMsg HttpRequestMethodNotSupportedException(Exception e){
        //logger.error(e.getMessage(), e);
        return AjaxMsg.error("405","错误的请求方式!");
    }

}
